package client;

import java.io.FileReader;
import java.io.IOException;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;

public class SAXReader extends DefaultHandler {

	private String basePath = "src\\client\\resources\\";
	protected String currentData;

	public SAXReader() {
		super();

	}

	public void start(String filePath) {
		XMLReader xr;
		try {
			xr = XMLReaderFactory.createXMLReader();
			xr.setContentHandler(this);
			xr.setErrorHandler(this);
			xr.setFeature("http://xml.org/sax/features/validation", true); //DTD validation
			FileReader r = new FileReader(basePath + filePath);
			xr.parse(new InputSource(r));

		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException io) {
			io.printStackTrace();
		}
	}

	public void startDocument() {
	}

	public void endDocument() {
	}

	public void startElement(String uri, String name, String qName,
			Attributes atts) {
		if ("".equals(uri))
			System.out.println("Start element: " + qName);
		else
			System.out.println("Start element: {" + uri + "}" + name);
		System.out.println("Att: " + atts.getValue("id"));
	}

	public void endElement(String uri, String name, String qName) {
		if ("".equals(uri))
			System.out.println("End element: " + qName);
		else
			System.out.println("End element:   {" + uri + "}" + name);
	}

	public void characters(char ch[], int start, int length) {
		StringBuffer buff = new StringBuffer();
		for (int i = start; i < start + length; i++) {
			switch (ch[i]) {
			case '\\':
				// System.out.print("\\\\");
				break;
			case '"':
				// System.out.print("\\\"");
				break;
			case '\n':
				// System.out.print("\\n");
				break;
			case '\r':
				// System.out.print("\\r");
				break;
			case '\t':
				// System.out.print("\\t");
				break;
			default:
				buff.append(ch[i]);
				break;
			}
		}
		currentData = buff.toString();
	}

}
